import { createRouter, createWebHashHistory } from 'vue-router';
import Layout from '@/Layout/index.vue';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/depManage',
    },
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/depManage',
      component: Layout,
      redirect: {
        name: 'depManage',
      },
      children: [
        {
          path: '',
          name: 'depManage',
          meta: {
            title: '部门管理',
          },
          component: () => import('@/views/depManage/index.vue'),
        },
      ],
    },
    {
      path: '/postManage',
      component: Layout,
      redirect: {
        name: 'postManage',
      },
      children: [
        {
          path: '',
          name: 'postManage',
          meta: {
            title: '岗位管理',
          },
          component: () => import('@/views/postManage/index.vue'),
        },
      ],
    },
    {
      path: '/staffManage',
      component: Layout,
      redirect: {
        name: 'staffManage',
      },
      children: [
        {
          path: '',
          name: 'staffManage',
          meta: {
            title: '员工管理',
          },
          component: () => import('@/views/staffManage/index.vue'),
        },
      ],
    },
    {
      path: '/probationManage',
      component: Layout,
      redirect: {
        name: 'probationManage',
      },
      children: [
        {
          path: '',
          name: 'probationManage',
          meta: {
            title: '试用期管理',
          },
          component: () => import('@/views/probationManage/index.vue'),
        },
      ],
    },
    {
      path: '/jobMoveManage',
      component: Layout,
      redirect: {
        name: 'jobMoveManage',
      },
      children: [
        {
          path: '',
          name: 'jobMoveManage',
          meta: {
            title: '岗位调动管理',
          },
          component: () => import('@/views/jobMoveManage/index.vue'),
        },
      ],
    },
    {
      path: '/staffDepartManage',
      component: Layout,
      redirect: {
        name: 'staffDepartManage',
      },
      children: [
        {
          path: '',
          name: 'staffDepartManage',
          meta: {
            title: '员工离职管理',
          },
          component: () => import('@/views/staffDepartManage/index.vue'),
        },
      ],
    },
    {
      path: '/reportFormManage',
      component: Layout,
      redirect: {
        name: 'reportFormManage',
      },
      children: [
        {
          path: '',
          name: 'reportFormManage',
          meta: {
            title: '报表管理',
          },
          component: () => import('@/views/reportFormManage/index.vue'),
        },
      ],
    },
  ],
});

// router.beforeEach((to) => {
//   const token = localStorage.token;
//   if (to.path.startsWith('/depManage') && !token) {
//     return '/login';
//   }
// });

const whiteList = ['/login'];

router.beforeEach((to, from, next) => {
  const token = localStorage.token;
  if (token) {
    if (to.path === '/login') {
      next('/depManage');
    }
    next();
  } else {
    if (to.path === '/login') {
      next();
    } else {
      next('/login');
    }
  }
});

export default router;
